#include "Program.h"

#include <cstdio>
#include <cstdlib>
#include <string.h>

using namespace COOLangage;

COOLangage::DeclarationFonction::DeclarationFonction(const char* name, Type* retour) {
  this->name = strdup(name);
  this->retour = new Type(*retour);
  this->corps = NULL;
}


COOLangage::DeclarationFonction::~DeclarationFonction() throw() {
  free(this->name);
  if(retour != NULL)
    delete this->retour;
  if(corps != NULL)
    delete corps;
}

const char* COOLangage::DeclarationFonction::getName() const {
  return this->name;
}

const Type* COOLangage::DeclarationFonction::getType() const {
  return this->retour;
}

const std::vector<Parametre> COOLangage::DeclarationFonction::getParametres() const {
  return this->parametres;
}

const Instruction* COOLangage::DeclarationFonction::getCorps() const {
  return this->corps;
}

// -----------------------------------------------------------------------------------------------------------------------
COOLangage::COOProgram::COOProgram() {
  // default on peut dire une fonction s'appelle main.
}

COOLangage::COOProgram::~COOProgram() throw() {
  // attention il faut liberer mainname;
  while(methods.size() > 0) {
    DeclarationFonction *p = methods.back();
    methods.pop_back();
    delete p;
  }

  while(varglobales.size() > 0) {
    DeclarationVariable *v = varglobales.back();
    varglobales.pop_back();
    delete v;
  }
}

const DeclarationFonction* COOLangage::COOProgram::getFonction(const char* name) const {
  unsigned int i=0;
  while(i < methods.size()) {
    DeclarationFonction *func = methods[i];
    if(strcmp(func->getName(), name))
      return func;
  }  
  return NULL;
}

std::vector<DeclarationVariable*> COOLangage::COOProgram::getVariables() const {
  return varglobales;
}


// -----------------------------------------------------------------------------------------------------------------------
